package com.fzf.org.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzf.org.dao.entities.UserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<UserDO> {

    /**
     * 需要在使用前保证传入的列表不会空且长度大于0
     * @param accounts 用户 账号 列表
     * @return
     */
    @Update({
            "<script>",
            "UPDATE t_user",
            "SET online = false",
            "WHERE account IN",
            "<foreach collection='accounts' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    int batchUpdateOnlineToFalse(@Param("accounts") List<String> accounts);

    @Select("SELECT account FROM t_user")
    List<String> selectAllAccounts();

    /**
     * 根据用户名username查用户的user_id
     * @param username 用户名
     * @return user_id 用户的id
     */
    @Select("SELECT user_id FROM t_user WHERE username = #{username}")
    Long selectUserIDFromUsername(@Param("username") String username);
}


